﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Global.Class.Library;
using BusinessLogicLayer;
using DataAccessLayer;
using System.Globalization;


namespace PresentationLayer
{
    public partial class MarketingProgram : Form, IMergeToolStrip, ICallToolStrip
    {

        #region <Implement Interface>

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public GlobalEnum.TaskID TaskID
        {
            get { return this.marketingProgramBLL.TaskID; }
        }


        public ToolStrip ChildToolStrip
        {
            get
            {
                return this.toolStripChildForm;
            }
            set
            {
                this.toolStripChildForm = value;
            }
        }





        #region Context toolbar


        public bool IsDirty
        {
            get { return this.marketingProgramBLL.IsDirty; }
        }

        public bool Closable
        {
            get { return true; }
        }

        public bool Newable
        {
            get { return true; }
            //get { return !this.marketingProgramBLL.ReadOnly; }
        }

        public bool Editable
        {
            get { return true; }
            //get { return this.marketingProgramBLL.Editable; }
        }

        public bool Deletable
        {
            get { return this.marketingProgramBLL.Editable; }
        }

        public bool Importable
        {
            get { return true; }
        }

        public bool Exportable
        {
            get { return false; }
        }

        public bool Verifiable
        {
            get { return this.marketingProgramBLL.Verifiable; }
        }

        public bool Unverifiable
        {
            get { return this.marketingProgramBLL.Unverifiable; }
        }

        public bool Printable
        {
            get { return false; }
        }

        /// <summary>
        /// Edit Mode: True, Read Mode: False
        /// </summary>
        private bool editableMode;
        private int lastMarketingProgramID;
        public bool EditableMode { get { return this.editableMode; } }
        /// <summary>
        /// This reverse of the EditableMode
        /// </summary>
        public bool ReadonlyMode { get { return !this.editableMode; } }

        /// <summary>
        /// Set the editableMode
        /// </summary>
        /// <param name="editableMode"></param>
        private void SetEditableMode(bool editableMode)
        {
            if (this.editableMode != editableMode)
            {
                this.lastMarketingProgramID = this.marketingProgramBLL.MarketingProgramID;
                this.editableMode = editableMode;
                this.NotifyPropertyChanged("EditableMode");
            }
        }


        private void CancelDirty(bool restoreSavedData)
        {
            try
            {
                if (restoreSavedData || this.marketingProgramBLL.MarketingProgramID <= 0)
                    this.marketingProgramBLL.Fill(this.lastMarketingProgramID);

                this.SetEditableMode(false);
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }



        #endregion Context toolbar



        #region ICallToolStrip

        public void Escape()
        {
            if (this.EditableMode)
            {
                if (this.IsDirty)
                {
                    DialogResult dialogResult = MessageBox.Show(this, "Do you want to save your change?", "Warning", MessageBoxButtons.YesNoCancel);
                    if (dialogResult == DialogResult.Yes)
                    {
                        this.Save();
                        if (!this.IsDirty) this.CancelDirty(false);
                    }
                    else if (dialogResult == DialogResult.No)
                    {
                        this.CancelDirty(true);
                    }
                    else
                        return;
                }
                else
                    this.CancelDirty(false);
            }
            else
                this.Close(); //Unload this module
        }

        //Can phai xem lai trong VB de xem lai this.SetEditableMode () khi can thiet
        public void New()
        {
            this.marketingProgramBLL.New();
            this.SetEditableMode(true);
        }

        public void Edit()
        {
            this.SetEditableMode(true);
        }

        public void Save()
        {
            this.marketingProgramBLL.Save();
        }

        public void Delete()
        {
            this.marketingProgramBLL.Delete();
        }

        public void Import()
        {
            lfImportExcells();
        }

        public void Export()
        {
            MessageBox.Show("Export");
        }

        public void Verify()
        {
            MessageBox.Show("Verify");
        }

        public void Print(PrintDestination printDestination)
        {
            MessageBox.Show("Print");
        }

        #endregion


        #endregion <Implement Interface>








        #region <Storage>
        private MarketingProgramBLL marketingProgramBLL;

        #endregion <Storage>


        public MarketingProgram()
        {
            try
            {
                InitializeComponent();

                InitializeTabControl();


                MarketingPaymentBLL a = new MarketingPaymentBLL();

                this.marketingProgramBLL = new MarketingProgramBLL();

                this.marketingProgramBLL.PropertyChanged += new PropertyChangedEventHandler(marketingProgramBLL_PropertyChanged);

            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }

        void marketingProgramBLL_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            this.NotifyPropertyChanged(e.PropertyName);
        }


        private void InitializeTabControl()
        {

            CustomTabControl customTabControlCustomerChannel = new CustomTabControl();
            CustomTabControl customTabControlMechanicScheme = new CustomTabControl();
            CustomTabControl customTabControlItemCategory = new CustomTabControl();

            customTabControlCustomerChannel.ImageList = this.imageListTabControl;
            customTabControlMechanicScheme.ImageList = this.imageListTabControl;
            customTabControlItemCategory.ImageList = this.imageListTabControl;

            customTabControlCustomerChannel.TabPages.Add("AddressArea", "Territory    ", 5);
            customTabControlCustomerChannel.TabPages[0].Controls.Add(this.dataGridViewAddressArea);
            this.dataGridViewAddressArea.Dock = DockStyle.Fill;

            customTabControlCustomerChannel.TabPages.Add("CustomerChannel", "Channel    ", 6);
            customTabControlCustomerChannel.TabPages[1].Controls.Add(this.dataGridViewCustomerChannel);
            this.dataGridViewCustomerChannel.Dock = DockStyle.Fill;

            customTabControlCustomerChannel.TabPages.Add("CuatomerName", "Customer", 0);
            customTabControlCustomerChannel.TabPages[2].Controls.Add(this.dataGridViewCustomerName);
            this.dataGridViewCustomerName.Dock = DockStyle.Fill;

            customTabControlCustomerChannel.DisplayStyle = TabStyle.VisualStudio;
            customTabControlCustomerChannel.DisplayStyleProvider.ImageAlign = ContentAlignment.MiddleLeft;
            this.splitContainerCustomerChannel.Panel2.Controls.Add(customTabControlCustomerChannel);
            customTabControlCustomerChannel.Dock = DockStyle.Fill;


            customTabControlMechanicScheme.TabPages.Add("MechanicScheme", "Mechanics ", 2);
            customTabControlMechanicScheme.TabPages[0].Controls.Add(this.dataGridViewMechanicScheme);
            this.dataGridViewMechanicScheme.Dock = DockStyle.Fill;


            customTabControlMechanicScheme.DisplayStyle = TabStyle.VisualStudio;
            customTabControlMechanicScheme.DisplayStyleProvider.ImageAlign = ContentAlignment.MiddleLeft;
            this.splitContainerMechanicScheme.Panel2.Controls.Add(customTabControlMechanicScheme);
            customTabControlMechanicScheme.Dock = DockStyle.Fill;


            customTabControlItemCategory.TabPages.Add("ItemCategory", "Category   ", 4);
            customTabControlItemCategory.TabPages[0].Controls.Add(this.dataGridViewItemCategory);
            this.dataGridViewItemCategory.Dock = DockStyle.Fill;

            customTabControlItemCategory.TabPages.Add("ItemCommodity", "Commodity", 9);
            customTabControlItemCategory.TabPages[1].Controls.Add(this.dataGridViewItemCommodity);
            this.dataGridViewItemCommodity.Dock = DockStyle.Fill;

            customTabControlItemCategory.DisplayStyle = TabStyle.VisualStudio;
            customTabControlItemCategory.DisplayStyleProvider.ImageAlign = ContentAlignment.MiddleLeft;
            this.splitContainerCustomerChannel.Panel1.Controls.Add(customTabControlItemCategory);
            customTabControlItemCategory.Dock = DockStyle.Fill;
        }

        private void MarketingProgram_Load(object sender, EventArgs e)
        {
            try
            {
                InitializeCommonControlBinding();

                InitializeDataGridBinding();

                InitializeReadOnlyModeBinding();
            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }


        #region <InitializeBinding>

        Binding salesmenIDBinding;

        Binding marketingProgramGroupIDBinding;
        Binding marketingProgramTypeIDBinding;
        Binding itemTypeIDBinding;
        Binding timeBaseIDBinding;

        Binding referenceBinding;
        Binding referenceCodeBinding;
        Binding marketingProgramNameBinding;
        Binding marketingProgramSubNameBinding;
        Binding remarksBinding;

        Binding beginingDateBinding;
        Binding endingDateBinding;

        Binding salesTargetBinding;
        Binding marketingBudgetBinding;

        Binding inActiveBinding;
        Binding isDirtyBinding;

        private void InitializeCommonControlBinding()
        {
            CommonMetaList commonMetaList = new CommonMetaList();


            ERmgrUIP.ListSalesmenNameDataTable listSalesmenNameDataTable = commonMetaList.GetSalesmenName();
            this.comboBoxSalesmenID.DataSource = listSalesmenNameDataTable;
            this.comboBoxSalesmenID.DisplayMember = listSalesmenNameDataTable.DescriptionOfficialColumn.ColumnName;
            this.comboBoxSalesmenID.ValueMember = listSalesmenNameDataTable.SalesmenIDColumn.ColumnName;
            this.salesmenIDBinding = this.comboBoxSalesmenID.DataBindings.Add("SelectedValue", this.marketingProgramBLL.MarketingProgramMaster, "SalesmenID", true);

            ERmgrUIP.ListMarketingProgramGroupDataTable listMarketingProgramGroupDataTable = commonMetaList.GetMarketingProgramGroup();
            this.comboBoxMarketingProgramGroupID.DataSource = listMarketingProgramGroupDataTable;
            this.comboBoxMarketingProgramGroupID.DisplayMember = listMarketingProgramGroupDataTable.DescriptionColumn.ColumnName;
            this.comboBoxMarketingProgramGroupID.ValueMember = listMarketingProgramGroupDataTable.MarketingProgramGroupIDColumn.ColumnName;
            this.marketingProgramGroupIDBinding = this.comboBoxMarketingProgramGroupID.DataBindings.Add("SelectedValue", this.marketingProgramBLL.MarketingProgramMaster, "MarketingProgramGroupID", true);

            ERmgrUIP.ListMarketingProgramTypeDataTable listMarketingProgramTypeDataTable = commonMetaList.GetMarketingProgramType();
            this.comboBoxMarketingProgramTypeID.DataSource = listMarketingProgramTypeDataTable;
            this.comboBoxMarketingProgramTypeID.DisplayMember = listMarketingProgramTypeDataTable.DescriptionColumn.ColumnName;
            this.comboBoxMarketingProgramTypeID.ValueMember = listMarketingProgramTypeDataTable.MarketingProgramTypeIDColumn.ColumnName;
            this.marketingProgramTypeIDBinding = this.comboBoxMarketingProgramTypeID.DataBindings.Add("SelectedValue", this.marketingProgramBLL.MarketingProgramMaster, "MarketingProgramTypeID", true);

            ERmgrUIP.ListItemTypeDataTable listItemTypeDataTable = commonMetaList.GetItemType();
            this.comboBoxItemTypeID.DataSource = listItemTypeDataTable;
            this.comboBoxItemTypeID.DisplayMember = listItemTypeDataTable.DescriptionColumn.ColumnName;
            this.comboBoxItemTypeID.ValueMember = listItemTypeDataTable.ItemTypeIDColumn.ColumnName;
            this.itemTypeIDBinding = this.comboBoxItemTypeID.DataBindings.Add("SelectedValue", this.marketingProgramBLL.MarketingProgramMaster, "ItemTypeID", true);

            ERmgrUIP.ListTimeBaseDataTable listTimeBaseDataTable = commonMetaList.GetTimeBase();
            this.comboBoxTimeBaseID.DataSource = listTimeBaseDataTable;
            this.comboBoxTimeBaseID.DisplayMember = listTimeBaseDataTable.DescriptionColumn.ColumnName;
            this.comboBoxTimeBaseID.ValueMember = listTimeBaseDataTable.TimeBaseIDColumn.ColumnName;
            this.timeBaseIDBinding = this.comboBoxTimeBaseID.DataBindings.Add("SelectedValue", this.marketingProgramBLL.MarketingProgramMaster, "TimeBaseID", true);



            this.referenceBinding = this.textBoxReference.DataBindings.Add("Text", this.marketingProgramBLL.MarketingProgramMaster, "Reference", true);
            this.referenceCodeBinding = this.textBoxReferenceCode.DataBindings.Add("Text", this.marketingProgramBLL.MarketingProgramMaster, "ReferenceCode", true);
            this.marketingProgramNameBinding = this.textBoxMarketingProgramName.DataBindings.Add("Text", this.marketingProgramBLL.MarketingProgramMaster, "MarketingProgramName", true);
            this.marketingProgramSubNameBinding = this.textBoxMarketingProgramSubName.DataBindings.Add("Text", this.marketingProgramBLL.MarketingProgramMaster, "MarketingProgramSubName", true);
            this.remarksBinding = this.textBoxRemarks.DataBindings.Add("Text", this.marketingProgramBLL.MarketingProgramMaster, "Remarks", true);


            this.beginingDateBinding = this.dateTimePickerBiginingDate.DataBindings.Add("Value", this.marketingProgramBLL.MarketingProgramMaster, "BeginingDate", true);
            this.endingDateBinding = this.dateTimePickerEndingDate.DataBindings.Add("Value", this.marketingProgramBLL.MarketingProgramMaster, "EndingDate", true);

            this.salesTargetBinding = this.textBoxSalesTarget.DataBindings.Add("Text", this.marketingProgramBLL.MarketingProgramMaster, "SalesTarget", true, DataSourceUpdateMode.OnValidation, 0, GlobalVariables.zeroDecimalFormat);
            this.marketingBudgetBinding = this.textBoxMarketingBudget.DataBindings.Add("Text", this.marketingProgramBLL.MarketingProgramMaster, "MarketingBudget", true, DataSourceUpdateMode.OnValidation, 0, GlobalVariables.zeroDecimalFormat);

            this.inActiveBinding = this.checkBoxInActive.DataBindings.Add("Checked", this.marketingProgramBLL.MarketingProgramMaster, "InActive", true);
            this.isDirtyBinding = this.checkBoxIsDirty.DataBindings.Add("Checked", this.marketingProgramBLL.MarketingProgramMaster, "IsDirty", true);



            this.salesmenIDBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.marketingProgramGroupIDBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.marketingProgramTypeIDBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.itemTypeIDBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.timeBaseIDBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);

            this.referenceBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.referenceCodeBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.marketingProgramNameBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.marketingProgramSubNameBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.remarksBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);

            this.beginingDateBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.endingDateBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);

            this.salesTargetBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.marketingBudgetBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);

            this.inActiveBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.isDirtyBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);

        }

        private void CommonControl_BindingComplete(object sender, BindingCompleteEventArgs e)
        {
            if (e.BindingCompleteState != BindingCompleteState.Success) { GlobalExceptionHandler.ShowExceptionMessageBox(this, e.ErrorText); e.Cancel = true; }
        }



        private void InitializeDataGridBinding()
        {
            //<dataGridViewMaster>
            this.dataGridViewMasterList.DataSource = this.marketingProgramBLL.MarketingProgramMasterList(new DateTime(2012, 1, 1), DateTime.Now);
            this.dataGridViewMasterList.ReadOnly = true; this.dataGridViewMasterList.AllowUserToAddRows = false; this.dataGridViewMasterList.AllowUserToDeleteRows = false;


            #region <dataGridViewDetail>
            CommonMetaList commonMetaList = new CommonMetaList();
            DataGridViewComboBoxColumn comboBoxColumn;


            //<dataGridViewItemCategory>
            this.dataGridViewItemCategory.AutoGenerateColumns = false;
            this.dataGridViewItemCategory.DataSource = this.marketingProgramBLL.ItemCategoryList;

            ERmgrUIP.ListItemCategoryDataTable listItemCategoryDataTable = commonMetaList.GetItemCategory(true);
            comboBoxColumn = (DataGridViewComboBoxColumn)this.dataGridViewItemCategory.Columns[listItemCategoryDataTable.ItemCategoryIDColumn.ColumnName];
            comboBoxColumn.DataSource = listItemCategoryDataTable;
            comboBoxColumn.DisplayMember = listItemCategoryDataTable.DescriptionColumn.ColumnName;
            comboBoxColumn.ValueMember = listItemCategoryDataTable.ItemCategoryIDColumn.ColumnName;


            //<dataGridViewItemCommodity>
            this.dataGridViewItemCommodity.AutoGenerateColumns = false;
            this.dataGridViewItemCommodity.DataSource = this.marketingProgramBLL.ItemCommodityList;

            ERmgrUIP.ListItemCommodityDataTable listItemCommodityDataTable = commonMetaList.GetItemCommodity(true);
            comboBoxColumn = (DataGridViewComboBoxColumn)this.dataGridViewItemCommodity.Columns[listItemCommodityDataTable.CommodityIDColumn.ColumnName];
            comboBoxColumn.DataSource = listItemCommodityDataTable;
            comboBoxColumn.DisplayMember = listItemCommodityDataTable.DescriptionOfficialColumn.ColumnName;
            comboBoxColumn.ValueMember = listItemCommodityDataTable.CommodityIDColumn.ColumnName;



            //<dataGridViewAddressArea>
            this.dataGridViewAddressArea.AutoGenerateColumns = false;
            this.dataGridViewAddressArea.DataSource = this.marketingProgramBLL.AddressAreaList;

            ERmgrUIP.ListAddressAreaDataTable listAddressAreaDataTable = commonMetaList.GetAddressArea(true);
            comboBoxColumn = (DataGridViewComboBoxColumn)this.dataGridViewAddressArea.Columns[listAddressAreaDataTable.AddressAreaIDColumn.ColumnName];
            comboBoxColumn.DataSource = listAddressAreaDataTable;
            comboBoxColumn.DisplayMember = listAddressAreaDataTable.DescriptionColumn.ColumnName;
            comboBoxColumn.ValueMember = listAddressAreaDataTable.AddressAreaIDColumn.ColumnName;



            //<dataGridViewCustomerChannel>
            this.dataGridViewCustomerChannel.AutoGenerateColumns = false;
            this.dataGridViewCustomerChannel.DataSource = this.marketingProgramBLL.CustomerChannelList;

            ERmgrUIP.ListCustomerChannelDataTable listCustomerChannelDataTable = commonMetaList.GetCustomerChannel(true);
            comboBoxColumn = (DataGridViewComboBoxColumn)this.dataGridViewCustomerChannel.Columns[listCustomerChannelDataTable.CustomerChannelIDColumn.ColumnName];
            comboBoxColumn.DataSource = listCustomerChannelDataTable;
            comboBoxColumn.DisplayMember = listCustomerChannelDataTable.DescriptionColumn.ColumnName;
            comboBoxColumn.ValueMember = listCustomerChannelDataTable.CustomerChannelIDColumn.ColumnName;



            //<dataGridViewCustomerName>
            this.dataGridViewCustomerName.AutoGenerateColumns = false;
            this.dataGridViewCustomerName.DataSource = this.marketingProgramBLL.CustomerNameList;

            ERmgrUIP.ListCustomerNameDataTable listCustomerNameDataTable = commonMetaList.GetCustomerName(true);
            comboBoxColumn = (DataGridViewComboBoxColumn)this.dataGridViewCustomerName.Columns[listCustomerNameDataTable.CustomerIDColumn.ColumnName];
            comboBoxColumn.DataSource = listCustomerNameDataTable;
            comboBoxColumn.DisplayMember = listCustomerNameDataTable.DescriptionColumn.ColumnName;
            comboBoxColumn.ValueMember = listCustomerNameDataTable.CustomerIDColumn.ColumnName;

            //--Display the second column for customer (Readonly): DescriptionOfficial -- Later: Try other way, instead of current DataGridViewComboBoxColumn.Datasource  
            comboBoxColumn = (DataGridViewComboBoxColumn)this.dataGridViewCustomerName.Columns[listCustomerNameDataTable.DescriptionOfficialColumn.ColumnName];
            comboBoxColumn.DataSource = listCustomerNameDataTable;
            comboBoxColumn.DisplayMember = listCustomerNameDataTable.DescriptionOfficialColumn.ColumnName;
            comboBoxColumn.ValueMember = listCustomerNameDataTable.CustomerIDColumn.ColumnName;




            //<dataGridViewMechanicScheme>
            this.dataGridViewMechanicScheme.AutoGenerateColumns = false;
            this.dataGridViewMechanicScheme.DataSource = this.marketingProgramBLL.MechanicSchemeList;

            ERmgrUIP.ListMechanicSchemeDataTable listMechanicSchemeDataTable = commonMetaList.GetMechanicScheme(true);
            comboBoxColumn = (DataGridViewComboBoxColumn)this.dataGridViewMechanicScheme.Columns[listMechanicSchemeDataTable.MechanicSchemeIDColumn.ColumnName];
            comboBoxColumn.DataSource = listMechanicSchemeDataTable;
            comboBoxColumn.DisplayMember = listMechanicSchemeDataTable.DescriptionColumn.ColumnName;
            comboBoxColumn.ValueMember = listMechanicSchemeDataTable.MechanicSchemeIDColumn.ColumnName;

            #endregion <dataGridViewDetail>
        }

        private void InitializeReadOnlyModeBinding()
        {
            List<Control> controlList = GlobalStaticFunction.GetAllControls(this);

            foreach (Control control in controlList)
            {
                if (control is TextBox) control.DataBindings.Add("Readonly", this, "ReadonlyMode");
                else if (control is ComboBox || control is DateTimePicker) control.DataBindings.Add("Enabled", this, "EditableMode");
                else if (control is DataGridView && !control.Equals(this.dataGridViewMasterList))
                {
                    control.DataBindings.Add("Readonly", this, "ReadonlyMode");
                    control.DataBindings.Add("AllowUserToAddRows", this, "EditableMode");
                    control.DataBindings.Add("AllowUserToDeleteRows", this, "EditableMode");
                }
            }

            this.dataGridViewMasterList.DataBindings.Add("Enabled", this, "ReadonlyMode");
        }

        #endregion <InitializeBinding>


        private void dataGridViewMasterList_SelectionChanged(object sender, EventArgs e)
        {
            if (this.dataGridViewMasterList.CurrentRow != null)
            {
                MarketingProgramDTS.MarketingProgramListingRow marketingProgramListingRow = ((DataRowView)this.dataGridViewMasterList.CurrentRow.DataBoundItem).Row as MarketingProgramDTS.MarketingProgramListingRow;
                if (marketingProgramListingRow != null) this.marketingProgramBLL.Fill(marketingProgramListingRow.MarketingProgramID);
            }
            else this.marketingProgramBLL.Fill(0);
        }


        private void dataGridViewMechanicScheme_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e)
        {
            if (e.RowIndex > -1 && e.RowIndex < this.dataGridViewMechanicScheme.Rows.Count && e.ColumnIndex > -1 && e.ColumnIndex < this.dataGridViewMechanicScheme.ColumnCount)
                e.ToolTipText = this.dataGridViewMechanicScheme.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
        }






        #region Import Excel

        private void lfImportExcells()
        {
            //Dim lForm As frmPublicDialogBox, lDlgExcellMapColumn As frmDlgExcellMapColumn, lSheetName As String, SQL As String, SQO As String, li As Long, i As Long
            //Dim lRol As ADODB.Recordset, lExcell As ADODB.Recordset, lBoxoffice As clsBoxofficeMaster, lBoxofficeDetail As clsBoxofficeDetail

            try
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Filter = "Excel File (.xlsx)|*.xlsx";

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    DialogMapExcelColumn dialogMapExcelColumn = new DialogMapExcelColumn(OleDbDatabase.MappingTaskID.MarketingProgram, openFileDialog.FileName);

                    if (dialogMapExcelColumn.ShowDialog() == DialogResult.OK)
                    {
                        this.marketingProgramBLL.lfImportExcells(openFileDialog.FileName);
                    }
                    dialogMapExcelColumn.Dispose();







                    //    Me.dlgPath.Filter = "Excel Files|*.xls": Me.dlgPath.DialogTitle = "Opening": Me.dlgPath.FileName = ""
                    //    Me.dlgPath.Flags = cdlOFNExplorer Or cdlOFNFileMustExist Or cdlOFNHideReadOnly
                    //    Me.dlgPath.CancelError = True: Me.dlgPath.ShowOpen: Me.dlgPath.FileName = Trim(Me.dlgPath.FileName)
                    //    If Me.dlgPath.FileName <> "" Then

                    //        Set lForm = New frmPublicDialogBox
                    //        lForm.vsFMaster.FormatString = "^#    |<Error #                                 |^Date              |<Film                                                      |<Cinema": lForm.vsFMaster.Rows = 1

                    //        '//0. CONNECT TO EXCEL FILES
                    //        Set DBIMPORTConn = ADOConnectToExCell(Me.dlgPath.FileName): If DBIMPORTConn Is Nothing Then GoTo ERR_HANDLER
                    //        '//1. MAPPING COLUMN
                    //        Set lDlgExcellMapColumn = New frmDlgExcellMapColumn: lSheetName = "BOJournal": lDlgExcellMapColumn.lSheetName = lSheetName: lDlgExcellMapColumn.lColumnTypeID = EColumnTypeID.EBoxofficeID: lDlgExcellMapColumn.lFileName = Me.dlgPath.FileName: lDlgExcellMapColumn.Show vbModal: If lContinue = 0 Then GoTo ERR_HANDLER
                    //        '//2. Import DATA

                    //        Set lRol = DFormConn.Execute("SELECT ListColumnExcellMap.ColumnID, ListColumnExcellMap.ColumnName, ListColumnBoxoffice.FieldName, ListColumnBoxoffice.OrderByID FROM ListColumnExcellMap INNER JOIN ListColumnBoxoffice ON ListColumnExcellMap.ColumnID = ListColumnBoxoffice.ColumnID WHERE ListColumnExcellMap.ColumnTypeID = " & EColumnTypeID.EBoxofficeID & " ORDER BY ListColumnBoxoffice.OrderByID")
                    //        SQL = " SELECT 1 AS NoUseField "
                    //        Do While Not lRol.BOF And Not lRol.EOF
                    //            SQL = SQL & ", " & "[" & lRol.Fields("ColumnName") & "]" & " AS " & lRol.Fields("FieldName")
                    //            If lRol.Fields("OrderByID") <> 0 Then SQO = SQO & IIf(SQO = "", " ORDER BY ", ", ") & "[" & lRol.Fields("ColumnName") & "]"
                    //            lRol.MoveNext
                    //        Loop

                    //        Set lExcell = ADORECNEW: Call lExcell.Open(SQL & " FROM [" & lSheetName & "$] " & SQO, DBIMPORTConn)

                    //        If Not lExcell.BOF And Not lExcell.EOF Then
                    //            Set lBoxoffice = New clsBoxofficeMaster
                    //            Do While Not lExcell.BOF And Not lExcell.EOF
                    //                lBoxoffice.BoxofficeID = -1: lBoxoffice.BoxofficeID = 0
                    //                lBoxoffice.BoxofficeDate = lExcell.Fields("BoxofficeDate")

                    //                If Not lfFilmBCLGet(lBoxoffice, lExcell.Fields("CinemaNameID"), lExcell.Fields("FilmNameID")) Then li = li + 1: lForm.vsFMaster.AddItem FormatNumber(li, 0) & vbTab & "BCL not found" & vbTab & lExcell.Fields("BoxofficeDate") & vbTab & lExcell.Fields("FilmNameID") & vbTab & lExcell.Fields("CinemaNameID"): GoTo ERR_NEXT

                    //                lBoxoffice.PreparedID = pOrganizationID.EmployeeID
                    //                lBoxoffice.SalesmenID = pOrganizationID.EmployeeID

                    //                Set lBoxofficeDetail = lBoxoffice.ChildAdd(lBoxoffice.BoxofficeDate + TimeSerial(19, 0, 0))
                    //                lBoxofficeDetail.CinemaRoomID = 1
                    //                lBoxofficeDetail.NoSession = lExcell.Fields("NoSession")



                    //                If Not lBoxoffice.Save(False) Then li = li + 1: lForm.vsFMaster.AddItem FormatNumber(li, 0) & vbTab & "Invalid save action" & vbTab & lExcell.Fields("BoxofficeDate") & vbTab & lExcell.Fields("FilmNameID") & vbTab & lExcell.Fields("CinemaNameID"): GoTo ERR_NEXT
                    //'//             If Not lBoxoffice.ApprovedChangemake(True) Then GoTo ERR_HANDLER
                    //ERR_NEXT:
                    //                lExcell.MoveNext
                    //            Loop
                    //        End If
                    //    End If

                    //    If li > 0 Then GoTo ERR_HANDLER

                    //ClassConn.CommitTrans
                    //    lfImportExcells = True
                    //    Call pfMessageBox(ENothing, "Congratulation!" & Chr(13) & Chr(13) & "File: " & Me.dlgPath.FileName & " is imported successfull!" & Chr(13) & Chr(13) & "Please press OK to finish.", EInformation)
                    //ERR_RESUME:
                    //    If li > 0 Then Call pfDialogBox(lForm, ENothing, "Error occurred while importing data. Please check data in prior to try again.")
                    //    Screen.MousePointer = vbDefault: Set DBIMPORTConn = Nothing: Exit Function
                    //ERR_HANDLER:
                    //ClassConn.RollbackTrans
                    //    If (Err.Number <> cdlCancel) Then psShowError: GoTo ERR_RESUME
                    //End Function
                }

            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }

        }








        #endregion Import Excel

       
    }
}
